package com.hucheng.reconciliation.biz;

import com.hucheng.common.utils.TimeUtils;
import com.hucheng.reconciliation.entity.AlipayOrderDO;
import com.hucheng.reconciliation.mapper.AlipayOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.*;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * @Author: 凉白开不加冰
 * @Version: 0.0.1V
 * @Date: 2019-07-30
 * @Description: 解析账单
 **/
@Slf4j
@Component
public class ReconciliationFileParserBiz {

    @Autowired
    private AlipayOrderMapper alipayOrderMapper;

//    /**
//     * 读取zip文件，不解压缩直接解析，支持文件名中文，解决内容乱码
//     * 获得输入流，文件为zip格式，
//     * 支付宝提供
//     * 20886126836996110156_20160906.csv.zip内包含
//     * 20886126836996110156_20160906_业务明细.csv
//     * 20886126836996110156_20160906_业务明细(汇总).csv
//     *
//     * @param path
//     * @return 读取zip文件，返回字符串
//     * @throws Exception
//     */
//    @SuppressWarnings("unchecked")
//    public boolean readZipToString(String path) throws Exception {
//        try {
//            InputStream in = new BufferedInputStream(new FileInputStream(path));
//            Charset gbk = Charset.forName("gbk");
//            ZipInputStream zin = new ZipInputStream(in, gbk);
//            BufferedReader br = new BufferedReader(new InputStreamReader(zin, "gbk"));
//            ZipEntry ze;
//            //循环读取zip中的cvs文件，无法使用jdk自带，因为文件名中有中文
//            while ((ze = zin.getNextEntry()) != null) {
//                if (ze.isDirectory()) {
//                    return false;
//                }
//                //获得cvs名字
//                String fileName = ze.getName();
//                log.info("支付宝账单文件 >>>>>>>>>> " + fileName);
//                //检测文件是否存在
//                if (fileName != null && fileName.indexOf(".") != -1 && !fileName.contains("汇总")) {
//                    String line;
//                    int count = 0;
//                    List<AlipayOrderDO> lists = new ArrayList<>();
//                    while ((line = br.readLine()) != null) {
//                        count++;
//                        //解析业务明细数据入库
//                        if (line.contains("业务明细列表结束")) {
//                            break;
//                        }
//                        if (count >= 6) {
//                            log.info("业务明细 >>>>>>> " + line);
//                            String[] strs = line.split(",", -1);
//                            if (strs.length < 1) {
//                                break;
//                            }
//                            AlipayOrderDO alipayOrderDO = new AlipayOrderDO();
//                            alipayOrderDO.setTradeNo(strs[0]);
//                            alipayOrderDO.setOrderNumber(strs[1]);
//                            alipayOrderDO.setBusinessType(strs[2]);
//                            alipayOrderDO.setGoodsName(strs[3]);
//                            alipayOrderDO.setDateCreated(TimeUtils.dateBySpecificDate(strs[4]));
//                            alipayOrderDO.setDateEnd(TimeUtils.dateBySpecificDate(strs[5]));
//                            alipayOrderDO.setMerNo(strs[6]);
//                            alipayOrderDO.setMerName(strs[7]);
//                            alipayOrderDO.setOperator(strs[8]);
//                            alipayOrderDO.setTerminalNumber(strs[9]);
//                            alipayOrderDO.setOtherAccount(strs[10]);
//                            alipayOrderDO.setOrderPrice(new BigDecimal(strs[11]));
//                            alipayOrderDO.setMerPrice(new BigDecimal(strs[12]));
//                            alipayOrderDO.setAlipayRewardPrice(new BigDecimal(strs[13]));
//                            alipayOrderDO.setJfTreasurePrice(new BigDecimal(strs[14]));
//                            alipayOrderDO.setAlipayDiscountPrice(new BigDecimal(strs[15]));
//                            alipayOrderDO.setMerDiscountPrice(new BigDecimal(strs[16]));
//                            alipayOrderDO.setHxPrice(new BigDecimal(strs[17]));
//                            alipayOrderDO.setRewardName(strs[18]);
//                            alipayOrderDO.setMerRewardPrice(new BigDecimal(strs[19]));
//                            alipayOrderDO.setCardPrice(new BigDecimal(strs[20]));
//                            alipayOrderDO.setRefundNo(strs[21]);
//                            alipayOrderDO.setGratuityPrice(new BigDecimal(strs[22]));
//                            alipayOrderDO.setFrPrice(new BigDecimal(strs[23]));
//                            alipayOrderDO.setRemark(strs[24]);
//                            lists.add(alipayOrderDO);
//                        }
//                    }
//                    if (lists.size() > 0) {
//                        //将数据插入数据库
//                        alipayOrderMapper.insert(lists);
//                    }
//                } else {
////                    String line;
////                    int count = 0;
////                    List<AlipayOrderSummaryDO> lists = new ArrayList<>();
////                    while ((line = br.readLine()) != null) {
////                        //解析业务汇总数据入库
////                        count++;
////                        //解析业务明细数据入库
////                        if (line.contains("业务汇总列表结束")) {
////                            break;
////                        }
////                        if (count >= 6) {
////                            log.info("业务汇总 >>>>>>> " + line);
////                            String[] strs = line.split(",");
////                            if (strs.length < 1) {
////                                break;
////                            }
////                            AlipayOrderSummaryDO alipayOrderSummaryDO = new AlipayOrderSummaryDO();
////                            alipayOrderSummaryDO.setMerNo(strs[0]);
////                            alipayOrderSummaryDO.setMerName();
////                            alipayOrderSummaryDO.setTotalOrderNumber();
////                            alipayOrderSummaryDO.setTotalRefundOrderNumber();
////                            alipayOrderSummaryDO.setTotalOrderPrice();
////                            alipayOrderSummaryDO.setTotalMerPrice();
////                            alipayOrderSummaryDO.setTotalAlipayDiscount();
////                            alipayOrderSummaryDO.setTotalMerDiscount();
////                            alipayOrderSummaryDO.setTotalCardPrice();
////                            alipayOrderSummaryDO.setTotalGratuityPrice();
////                            alipayOrderSummaryDO.setTotalFrPrice();
////                            alipayOrderSummaryDO.setTotalSsPrice();
////                            lists.add(alipayOrderSummaryDO);
////                        }
////                    }
//                    //将数据插入数据库
//
//                }
//            }
//            //关闭流
//            in.close();
//            br.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return false;
//        }
//        return true;
//    }

    public boolean parser(String downUrl) {
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(downUrl));
            Charset gbk = Charset.forName("gbk");
            ZipInputStream zin = new ZipInputStream(in, gbk);
            BufferedReader br = new BufferedReader(new InputStreamReader(zin, "gbk"));
            ZipEntry ze;
            //循环读取zip中的cvs文件，无法使用jdk自带，因为文件名中有中文
            while ((ze = zin.getNextEntry()) != null) {
                if (ze.isDirectory()) {
                    return false;
                }
                //获得cvs名字
                String fileName = ze.getName();
                log.info("支付宝账单文件 >>>>>>>>>> " + fileName);
                //检测文件是否存在
                if (fileName != null && fileName.indexOf(".") != -1 && !fileName.contains("汇总")) {
                    String line;
                    int count = 0;
                    List<AlipayOrderDO> lists = new ArrayList<>();
                    while ((line = br.readLine()) != null) {
                        count++;
                        //解析业务明细数据入库
                        if (line.contains("业务明细列表结束")) {
                            break;
                        }
                        if (count >= 6) {
                            log.info("业务明细 >>>>>>> " + line);
                            String[] strs = line.split(",", -1);
                            if (strs.length < 1) {
                                break;
                            }

                            AlipayOrderDO alipayOrderDO = new AlipayOrderDO();
                            alipayOrderDO.setTradeNo(strs[0]);
                            alipayOrderDO.setOrderNumber(strs[1]);
                            alipayOrderDO.setBusinessType(strs[2]);
                            alipayOrderDO.setGoodsName(strs[3]);
                            alipayOrderDO.setDateCreated(TimeUtils.dateBySpecificDate(strs[4]));
                            alipayOrderDO.setDateEnd(TimeUtils.dateBySpecificDate(strs[5]));
                            alipayOrderDO.setMerNo(strs[6]);
                            alipayOrderDO.setMerName(strs[7]);
                            alipayOrderDO.setOperator(strs[8]);
                            alipayOrderDO.setTerminalNumber(strs[9]);
                            alipayOrderDO.setOtherAccount(strs[10]);
                            alipayOrderDO.setOrderPrice(new BigDecimal(strs[11]));
                            alipayOrderDO.setMerPrice(new BigDecimal(strs[12]));
                            alipayOrderDO.setAlipayRewardPrice(new BigDecimal(strs[13]));
                            alipayOrderDO.setJfTreasurePrice(new BigDecimal(strs[14]));
                            alipayOrderDO.setAlipayDiscountPrice(new BigDecimal(strs[15]));
                            alipayOrderDO.setMerDiscountPrice(new BigDecimal(strs[16]));
                            alipayOrderDO.setHxPrice(new BigDecimal(strs[17]));
                            alipayOrderDO.setRewardName(strs[18]);
                            alipayOrderDO.setMerRewardPrice(new BigDecimal(strs[19]));
                            alipayOrderDO.setCardPrice(new BigDecimal(strs[20]));
                            alipayOrderDO.setRefundNo(strs[21]);
                            alipayOrderDO.setGratuityPrice(new BigDecimal(strs[22]));
                            alipayOrderDO.setFrPrice(new BigDecimal(strs[23]));
                            alipayOrderDO.setRemark(strs[24]);
                            lists.add(alipayOrderDO);
                        }
                    }
                    if (lists.size() > 0) {
                        //将数据插入数据库
                        alipayOrderMapper.insert(lists);
                    }
                }
            }
            //关闭流
            in.close();
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
